﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using GPSTrackingWebsiteManager.ClassManager;

namespace GPSTrackingWebsiteManager.Pages
{
    public partial class GPSManager : System.Web.UI.Page
    {
        private void GetAllGPS()
        {
            SessionManager objSessionManager = (SessionManager)HttpContext.Current.Session["SessionManager"];         
            System.Data.DataSet ds = GPSTrackingDBV2Custom.Data.DataRepository.GPSStockProvider.GetGPSGroupsByUsername(objSessionManager.CompanyID, User.Identity.Name);
            GPSGrid.DataSource = ds;
        }

        private GPSTrackingDBV2.Entities.GpsStock Invoke_Entity(Hashtable Entity)
        {
            GPSTrackingDBV2.Entities.GpsStock GpsStockEntity = new GPSTrackingDBV2.Entities.GpsStock();
            GpsStockEntity.GpsImei = Convert.ToString(Entity[GPSTrackingDBV2.Entities.GpsStockColumn.GpsImei.ToString()]);
            GpsStockEntity.Name = Convert.ToString(Entity[GPSTrackingDBV2.Entities.GpsStockColumn.Name.ToString()]);          
            GpsStockEntity.Description = Convert.ToString(Entity[GPSTrackingDBV2.Entities.GpsStockColumn.Description.ToString()]);            

            return GpsStockEntity;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            

        }

        protected void GPSModelGrid_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {

        }

        protected void GPSModelGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            GetAllGPS();
        }
        

        protected void GPSGrid_UpdateCommand(object sender, GridCommandEventArgs e)
        {
            try
            {
                GridEditableItem item = e.Item as GridEditableItem;
                Hashtable Entity = new Hashtable();
                item.ExtractValues(Entity);

                GPSTrackingDBV2.Entities.GpsStock GpsStockEntity = Invoke_Entity(Entity);

                GPSTrackingDBV2.Entities.GpsStock GpsStockEntityUpdate = GPSTrackingDBV2.Data.DataRepository.GpsStockProvider.GetByGpsImei(GpsStockEntity.GpsImei);
                GpsStockEntityUpdate.Name = GpsStockEntity.Name;
                GpsStockEntityUpdate.Description = GpsStockEntity.Description;

                GpsStockEntityUpdate.OriginalGpsImei = GpsStockEntityUpdate.GpsImei;

                GPSTrackingDBV2.Data.DataRepository.GpsStockProvider.Update(GpsStockEntityUpdate);
            }
            catch (Exception ex)
            {
                ClassManager.CustomExceptionManager.Generate_Error_Object_RadGrid("Error - " + ex.Message, ref GPSGrid);
            }
        }

       
    }
}